package com.wenwei.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.umeng.message.IUmengCallback;
import com.umeng.message.PushAgent;
import com.wenwei.bean.LoginBean;
import com.wenwei.parking.AtyContainer;
import com.wenwei.parking.BaseActivity;
import com.wenwei.parking.Constant;
import com.wenwei.parking.R;
import com.wenwei.utils.RogerSPUtils;
import com.wenwei.view.CustomDelDialog;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/22.
 */
public class SettingActivity extends BaseActivity {
    @Bind(R.id.base_title)
    TextView baseTitle;
@Bind(R.id.switch_id)
    Switch aSwitch;
    @Override
    public int setLayoutResourceId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        super.initView();
        baseTitle.setText("设置");

        SharedPreferences sharedPreferences = getSharedPreferences("configuration", 0);
        String name = sharedPreferences.getString("is_open","");
        if(name==null || name.equals("")){
            //取不到配置信息则为第一次安装，进行引导页
            aSwitch.setChecked(true);
        }else{
            if(name.equals("t")){
                aSwitch.setChecked(true);
            }else{
                aSwitch.setChecked(false);
            }
        }
        aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(!isChecked){
                    PushAgent mPushAgent = PushAgent.getInstance(mContext);
                    mPushAgent.setNoDisturbMode(23,0,22,59);
                    mPushAgent.disable(new IUmengCallback() {
                        @Override
                        public void onSuccess() {
                            Logger.d("关闭成功");
                            SharedPreferences sharedPreferences = getSharedPreferences("configuration", 0);
                            SharedPreferences.Editor  editor  =  sharedPreferences.edit();
                            editor.putString("is_open","f");
                            editor.commit();
                        }

                        @Override
                        public void onFailure(String s, String s1) {
                            Logger.d("关闭失败："+s+","+s1);
                        }
                    });
                }else {
                    PushAgent mPushAgent = PushAgent.getInstance(mContext);
                    mPushAgent.setNoDisturbMode(0,0,0,0);
                    mPushAgent.enable(new IUmengCallback() {
                        @Override
                        public void onSuccess() {
                            Logger.d("成功");
                            SharedPreferences sharedPreferences = getSharedPreferences("configuration", 0);
                            SharedPreferences.Editor  editor  =  sharedPreferences.edit();
                            editor.putString("is_open","t");
                            editor.commit();
                        }

                        @Override
                        public void onFailure(String s, String s1) {
                            Logger.d("关闭");
                        }
                    });
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
    private CustomDelDialog mExitLoginDialog;
    @OnClick({R.id.use_rl,R.id.suggestion_rl,R.id.check_version,R.id.clear_cache,R.id.base_back, R.id.bind_btn,R.id.update_pw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.use_rl:
                Bundle b = new Bundle();
                b.putInt("type", Constant.TEXT_TYPE_USE);
                goActivity(mContext,ActivityTv.class,b);
                break;
            case R.id.suggestion_rl:
                goActivity(mContext,SuggestionActivity.class);
                break;
            case R.id.check_version:
                showToast("已是最新版");
                break;
            case R.id.clear_cache:
                showToast("已清除缓存");
                break;
            case R.id.base_back:
                finish();
                break;
            case R.id.bind_btn:
                if (mExitLoginDialog == null) {
                    mExitLoginDialog = new CustomDelDialog(mContext);
                    mExitLoginDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    mExitLoginDialog.setDialogMsg("残忍退出？");
                    mExitLoginDialog.setDelDialogEvent(new CustomDelDialog.DelDialogEvent() {
                        @Override
                        public void onDelete() {
                            mExitLoginDialog.dismiss();
                            RogerSPUtils.clear(mContext);
                            AtyContainer.getInstance().finishAllActivity();
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        goActivity(mContext,LoginActivity.class);
                                    } catch (Exception e) {
                                    }
                                }
                            }).start();
                        }

                        @Override
                        public void onCancel() {
                            mExitLoginDialog.dismiss();
                        }
                    });
                }
                mExitLoginDialog.show();
                break;
            case R.id.update_pw:
                Bundle forgetBundle = new Bundle();
                forgetBundle.putString("type","forget");
                goActivity(mContext,RegisterActivity.class,forgetBundle);
                break;
        }
    }
}
